package com.pyip.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyip.reggie.Entity.Setmeal;
import com.pyip.reggie.common.R;
import com.pyip.reggie.dto.SetmealDto;
import com.pyip.reggie.service.SetmealDishService;
import com.pyip.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CacheManager cacheManager;


    /**
     * 保存套餐信息
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    @RequestMapping(method = RequestMethod.POST)
    public R<String> saveSetMeal(@RequestBody SetmealDto setmealDto){
        return setmealService.saveSetMealWithSetMealDish(setmealDto);
    }

    /**
     * 分布查询数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> getPage(Long page, Long pageSize, String name){
        return setmealService.getPage(page,pageSize,name);
    }

    /**
     * 通过ids列表删除套餐setmeal表和套餐菜品关联表setMealDish
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    @RequestMapping(method = RequestMethod.DELETE)
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        return setmealService.deleteSetMealAndDishByIds(ids);
    }

    /**
     * 根据categoryiD和status查找list集合
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId +'_'+ #setmeal.status")
    @ApiOperation(value = "套餐条件查询接口")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public R<List<Setmeal>> getList(Setmeal setmeal){
        return setmealService.getList(setmeal);
    }

}
